﻿using SYJ.BLL;
using SYJ.Common.web;
using SYJ.IBLL;
using SYJ.Model;
using SYJ.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Dapper;
using System.Collections;
using System.IO;
using System.Web;
using System.Globalization;

namespace SYJ.Logic.Controllers.admin
{
    public class NoticeMgrController : BaseController
    {
        INoticeBLL bll = new NoticeBLL();
        INoticeTypeBLL tbll = new NoticeTypeBLL();
        public ActionResult Index()
        {
            ViewBag.NoticeTypeList = tbll.GetAll();
            return View();
        }
       
        public ActionResult GetNoticeList(QueryModel query, string title, string author, int? noticetype)
        {
            long count = 0;
          
            if (!string.IsNullOrEmpty(title))
            {
                query.SqlWhere = " and a.title like ?title ";
                //query.Param = new { title = "%" + title + "%" };
                query.Param.Add("title", "%" + title + "%");
            }
            if (!string.IsNullOrEmpty(author))
            {
                query.SqlWhere += " and c.author like ?author ";
                query.Param.Add("author", "%" + author + "%");
            }
            if (noticetype!=null && noticetype > 0)
            {
                query.SqlWhere += " and b.id = ?type ";
                query.Param.Add("type", noticetype);
            }
            List<Notice> list = bll.GetList(query, out count);
            var respose = JsonLayuiGridData<Notice>.Create(query, list, count, "");
            return Json(respose, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult AddOrUp(int? id)
        {
            Notice role = new Notice();
            ViewBag.NoticeTypeList = tbll.GetAll();
            if (id != null)
            {
                role = bll.GetModel(Convert.ToInt32(id));
            }
            return View(role);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUp(Notice model)
        {
            model.user_id = AppContext.Current.User.id;
            if (model.id > 0)
            {
                return Json(bll.Update(model), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(bll.Insert(model), JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Delete(int id)
        {
            bool delete = bll.Delete(new Notice() { id = id });
            return Json(delete, JsonRequestBehavior.AllowGet);
        }

        #region 类型管理
        public ActionResult NoticeType()
        {
            return View();
        }
        public ActionResult GetNoticeTypeList(QueryModel query, string title)
        {
            long count = 0;
            var param = new DynamicParameters();
            if (!string.IsNullOrEmpty(title))
            {
                query.SqlWhere = " and title like ?title ";
                param.Add("title", "%" + title + "%");
            }
            query.Param = param;
            List<NoticeType> list = tbll.GetPage(query, out count);
            var respose = JsonLayuiGridData<NoticeType>.Create(query, list, count, "");
            return Json(respose, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult AddOrUpType(int? id)
        {
            NoticeType role = new NoticeType();
            ViewBag.NoticeTypeList = tbll.GetAll();
            if (id != null)
            {
                role = tbll.GetModel(Convert.ToInt32(id));
            }
            return View(role);
        }
        [HttpPost]
        public ActionResult AddOrUpType(NoticeType model)
        {
            if (model.id > 0)
            {
                return Json(tbll.Update(model), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(tbll.Insert(model), JsonRequestBehavior.AllowGet);
            }
        }
        public ActionResult DeleteType(int id)
        {
            bool delete = tbll.Delete(new NoticeType() { id = id });
            return Json(delete, JsonRequestBehavior.AllowGet);
        }

        #endregion 


      
    }
}
